package org.example.javafx.Application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.example.HttpClient.impl.JAxiosUtils;
import org.example.javafx.WebApi.BaseURL;

import java.io.IOException;
import java.util.Map;


public class WebApiApplication extends Application {
    private static final Text text = new Text();

    // 按钮点击事件
    // 事件处理函数
    private static void OnAction(ActionEvent event)  {
        try {
            JAxiosUtils.Build
                    .Post(BaseURL.YES_API.getUrl())
                    .setParameter(
                            Map.of("s", "App.Hello.World",
                                    "name", "YesApi",
                                    "app_key", "4D8BD50D6335922AD66732F1F5045C4E",
                                    "sign", "527890A765471599E817A8BC87887EAD")
                    )
//                    .setJsonBody("json",
//                            Map.of("s", "App.Hello.World",
//                                    "name", "YesApi",
//                                    "app_key", "4D8BD50D6335922AD66732F1F5045C4E",
//                                    "sign", "527890A765471599E817A8BC87887EAD")
//                    )
                    .Then(WebApiApplication::HttpThenFunctional)
                    .Catch(WebApiApplication::HttpCatchFunctional)
                    .AsyncRun();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void HttpThenFunctional(CloseableHttpResponse response) {

        String responseBody = null;
        try {
            responseBody = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(responseBody);
        text.setText(responseBody);
    }

    private static void HttpCatchFunctional(CloseableHttpResponse response) {
        String responseBody = null;
        try {
            responseBody = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.err.println(responseBody);
        text.setText("错误：" + responseBody);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(newScene());
        stage.setTitle("WebApi");
        stage.setWidth(300);
        stage.setHeight(200);
        stage.setMaxWidth(450);
        stage.setMaxHeight(200);
        stage.show();
    }

    // 创建场景
    private Scene newScene() {
        AnchorPane root = new AnchorPane();

        Button button = new Button();
        button.setText("goto");
        button.setFont(new Font(20));

        Button button1 = new Button();
        button1.setLayoutX(100);
        button1.setText("cs");
        button1.setFont(new Font(20));

        text.setFont(new Font(20));
        text.setText("Hello World");
        text.setY(60);

        text.setWrappingWidth(200);

        button.setOnAction(WebApiApplication::OnAction);

        root.getChildren().addAll(button,button1, text);
        return new Scene(root);
    }

}
